package com.dd.ent.car.common.player;

import android.content.Context;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.os.Looper;

import com.dd.ent.car.R;


/**
 * Author:LiuJianSheng
 * Date:2016/6/21
 */
public class MediaPlayerUtils {
    /**
     * 使背景音乐静音(暂停)
     *
     * @param context
     * @param bMute   是否静音，true 静音
     * @return
     */
    public static boolean muteAudioFocus(Context context, boolean bMute) {
        if (context == null) {
            return false;
        }
        if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.FROYO) {
            // 2.1以下的版本不支持下面的API：requestAudioFocus和abandonAudioFocus
            return false;
        }
        boolean bool = false;
        AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        if (bMute) {
            int result = am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
            bool = result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED; // 不得获取音频焦点
        } else {
            int result = am.abandonAudioFocus(null);
            bool = result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
        }
        return bool;
    }

    /**
     * MobileNet下显示dialog
     *
     * @param context
     * @param listener
     */
    public static void showMobileNetNotifyDialog(Context context, final OnCheckNetworkDialogListener listener) {
        String contentStr = context.getString(R.string.feed_wifi_notify_tag_has_mobile);

        // 左按钮--停止播放
        String leftBtnStr = context.getString(R.string.feed_wifi_notify_btn_stop);
        DialogInterface.OnClickListener lListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                listener.onLeftBtnClick();
            }
        };

        // 右按钮 --继续播放
        String rightBtnStr = context.getString(R.string.feed_wifi_notify_btn_continue);
        DialogInterface.OnClickListener rListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                FeedPlayerParameters.instance().setNotCheckMobileNetAgain(true);
                dialog.dismiss();
                listener.onRightBtnClick();
            }
        };

        // 物理返回键
        DialogInterface.OnCancelListener cListener = new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                dialog.dismiss();
                listener.onBackKeyDown();
            }
        };

        FeedDataUtils.showWifiNotifyDialog(contentStr, leftBtnStr, rightBtnStr, lListener, rListener, cListener, context);
    }

    public interface OnCheckNetworkDialogListener {
        void onLeftBtnClick();

        void onRightBtnClick();

        void onBackKeyDown();
    }

    public static boolean isInUiThread() {
        return (Looper.getMainLooper().getThread() == Thread.currentThread());
    }

    public static String secondToClickString(int second) {
        if (second < 0) return "00:00";
        if (second == 0) return "00:00";

        int minute = second / 60;
        if (minute == 0) {
            return "00:" + doubleDigit(second);
        }
        int hour = minute / 60;
        int secondDigit = second % 60;
        if (hour == 0) {
            return doubleDigit(minute) + ":" + doubleDigit(secondDigit);
        } else {
            int minuteDigit = minute % 60;
            return doubleDigit(minuteDigit) + ":" + doubleDigit(secondDigit);
        }
    }

    private static String doubleDigit(int time) {
        if (time < 10) {
            return "0" + time;
        } else {
            return "" + time;
        }
    }
}
